class Solution {
public:
    int longestEqualSubarray(vector<int>& nums, int k) {
        unordered_map<int,vector<int>> pos;
        for(int i=0;i<nums.size();++i) pos[nums[i]].emplace_back(i);
        int ans=0;
        for(auto &[_,vec]:pos)
        {
            for(int left=0,right=0;right<vec.size();++right)
            {
                while(vec[right]-vec[left]-(right-left)>k) ++left;
                ans=max(ans,right-left+1);
            }
        }
        return ans;
    }
};